﻿using System;
using System.IO;
using System.Xml.Serialization;

namespace T68Control.Procdure
{
    /// <summary>
    /// 测试数量统计类
    /// </summary>
    [XmlRoot("测试数量统计")]
    [Serializable]
    public class Counter
    {
        private static string file = "counter.xml";

        [XmlElement("NG数量")]
        public int NgCount { get; set; }

        [XmlElement("OK数量")]
        public int OkCount { get; set; }

        public static Counter Load()
        {
            if (!File.Exists(file))
            {
                Counter c = new Counter
                {
                    OkCount = 1,
                    NgCount = 1
                };
                Save(c);
                return c;
            }
            else
            {
                XmlSerializer serializer = new XmlSerializer(typeof(Counter));

                TextReader reader = new StreamReader(file);
                // 初始化默认值

                var obj = serializer.Deserialize(reader);
                reader.Close();
                return obj as Counter;
            }
        }

        public static void Save(Counter counter)
        {
            if (counter is null)
            {
                throw new ArgumentNullException("参数错误");
            }
            XmlSerializer serializer = new XmlSerializer(typeof(Counter));
            TextWriter writer = new StreamWriter(file);
            serializer.Serialize(writer, counter);
            writer.Close();
        }
    }
}